//
//  SuggestionViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/8/2.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SuggestionViewController.h"
#import "JYHTextView.h"

@interface SuggestionViewController ()<UITextViewDelegate>

@property (strong, nonatomic) JYHTextView *textView;
@property (strong, nonatomic) UILabel *tipLabel;
@property (strong, nonatomic) UIImageView *submitIV;
@property (strong, nonatomic) UILabel *submitL;

@property (strong, nonatomic) UILabel *suggestLabel;
@property (strong, nonatomic) UIImageView *suggestImageView;
@property (strong, nonatomic) UILabel *suggestContactLabel;

@property (strong, nonatomic) UIButton *phoneBtn;

@end

@implementation SuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = Colorf2f2f2;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"意见反馈";
    
    [self prepareUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:@"UITextViewTextDidChangeNotification" object:self.textView];

}

#pragma mark -lazy
- (void)submitClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"意见反馈提交");
    
    if (self.textView.text.length < 1) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"请填写您的宝贵意见"];
        return;
    }
    
    WS(weakSelf);
    [QNetWorkRequest feedbackparameter:@{@"phone":[CommonUserInfo sharedInstance].phone, @"content":self.textView.text} Success:^(id response) {
        [[KYAlertView sharedInstance] showAlertViewWithMessage:@"提交成功"];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } Failure:^(NSError *error) {
    }];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark - Notification Method
-(void)textFieldEditChanged:(NSNotification *)obj
{
    UITextView *textview = (UITextView *)obj.object;
    NSString *toBeString = textview.text;
    NSString *lang = [textview.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textview markedTextRange];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限
        UITextPosition *position = [textview positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > 160)
            {
                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:160];
                if (rangeIndex.length == 1)
                {
                    textview.text = [toBeString substringToIndex:160];
                }
                else
                {
                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 160)];
                    textview.text = [toBeString substringWithRange:rangeRange];
                }
            }
        }
        
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > 160)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:160];
            if (rangeIndex.length == 1)
            {
                textview.text = [toBeString substringToIndex:160];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 160)];
                textview.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    
    if (160 - toBeString.length >= 1 && toBeString.length < 160) {
        self.tipLabel.text = [NSString stringWithFormat:@"还可以输入%lu个字", 160 - toBeString.length];
    }else {
        self.tipLabel.text = @"已经输入160个字";
    }
    
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    return YES;
}

- (void)prepareUI {
    
    [self.view addSubview:self.textView];
    [self.view addSubview:self.tipLabel];
    [self.view addSubview:self.submitIV];
    [self.view addSubview:self.submitL];
    [self.view addSubview:self.suggestLabel];
    [self.view addSubview:self.suggestImageView];
    [self.view addSubview:self.suggestContactLabel];
    [self.view addSubview:self.phoneBtn];
    __weak typeof(self) weakSelf = self;
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(jisuan(30));
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(30));
        
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(30));
        make.height.mas_equalTo(jisuan(600));
        
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(weakSelf.textView.mas_bottom).with.offset(-jisuan(42));
        make.right.mas_equalTo(weakSelf.textView.mas_right).with.offset(-jisuan(42));
    }];
    
    [self.submitIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.textView.mas_bottom).with.offset(jisuan(30));
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX);
        make.width.mas_equalTo(jisuan(550));
        make.height.mas_equalTo(jisuan(106));
    }];
    [self.submitL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.submitIV.mas_centerY).with.offset(-jisuan(4));
        make.centerX.mas_equalTo(weakSelf.submitIV.mas_centerX);
    }];
    
    
    [self.suggestLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(30));
        make.right.mas_equalTo(weakSelf.view.mas_right).with.offset(-jisuan(30));
        make.top.mas_equalTo(weakSelf.submitIV.mas_bottom).with.offset(jisuan(100));
        
    }];
    [self.suggestImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left).with.offset(jisuan(30));
        make.top.mas_equalTo(weakSelf.suggestLabel.mas_bottom).with.offset(jisuan(15));
        make.width.mas_equalTo(jisuan(40));
        make.height.mas_equalTo(jisuan(40));
        
    }];
    [self.phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.suggestImageView.mas_right).with.offset(jisuan(15));
 make.centerY.mas_equalTo(weakSelf.suggestImageView.mas_centerY);
    }];
}


#pragma mark -lazy
- (JYHTextView *)textView {
    if (_textView == nil) {
        
        _textView = [[JYHTextView alloc] init];
        _textView.placeholder = @"想对我们说点什么...";
        _textView.delegate = self;
        _textView.font = Font(32);
        _textView.backgroundColor = WhiteColor;
        _textView.layer.borderColor = UIColor.grayColor.CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.layer.cornerRadius = 5;
        _textView.layer.masksToBounds = YES;
        
        _textView.textContainerInset = UIEdgeInsetsMake(jisuan(42), jisuan(39), jisuan(42), jisuan(42));
    }
    return _textView;
}

- (UILabel *)tipLabel {
    if (_tipLabel == nil) {
        
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = Font(24);
        _tipLabel.textColor = Color999999;
        _tipLabel.text = @"还可以输入160个字";
        
    }
    return _tipLabel;
}

- (UIImageView *)submitIV {
    
    if (!_submitIV) {
        
        _submitIV = [[UIImageView alloc] init];
        _submitIV.contentMode = UIViewContentModeScaleAspectFit;
        _submitIV.image = [UIImage imageNamed:@"redBtnBG"];
        _submitIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(submitClick:)];
        [_submitIV addGestureRecognizer:gesture];
    }
    
    return _submitIV;
}

- (UILabel *)submitL {
    
    if (_submitL == nil) {
        
        _submitL = [[UILabel alloc] init];
        _submitL.text = @"提交";
        _submitL.textColor = WhiteColor;
        _submitL.font = Font(32);
        _submitL.textAlignment = NSTextAlignmentCenter;
    
    }
    
    return _submitL;
}

- (UILabel *)suggestLabel {
    
    if (_suggestLabel == nil) {
        
        _suggestLabel = [[UILabel alloc] init];
        _suggestLabel.text = @"还可以通过以下方式反馈给我们哦！";
        _suggestLabel.font = Font(24);
        _suggestLabel.textColor = Color333333;
        _suggestLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _suggestLabel;
}

- (UIImageView *)suggestImageView {
    
    if (_suggestImageView == nil) {
        
        _suggestImageView = [[UIImageView alloc] init];
        _suggestImageView.image = [UIImage imageNamed:@"settingPhoneBG"];
    }
    
    return _suggestImageView;
}

-(UIButton *)phoneBtn{
    if (!_phoneBtn) {
        _phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_phoneBtn setTitle:@"官方电话：010-53882711" forState:UIControlStateNormal];
        _phoneBtn.titleLabel.textColor = Color999999;
        _phoneBtn.titleLabel.font = Font(24);
        [_phoneBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _phoneBtn;
}
-(void)phoneBtnClick:(UIButton *)sender{
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"010-53882711"];[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    
}
- (UILabel *)suggestContactLabel {
    
    if (_suggestContactLabel == nil) {
        _suggestContactLabel = [[UILabel alloc] init];
        _suggestContactLabel.textColor = Color999999;
        _suggestContactLabel.text = @"官方电话：010-53882711";
        _suggestContactLabel.font = Font(24);
        _suggestContactLabel.textAlignment = NSTextAlignmentLeft;
    }
    
    return _suggestContactLabel;
}


#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
